import UIKit

//1、定义

    enum CompassPoint{
        //成员值，成员不会有初始值，如 0，1，2，3，每个成员就是一个值 类型是CompassPoint
        case north
        case south
        case east
        case west
    }

    //也可以写成这样
    enum Plant{
        case mercury,venus, earth ,mars, jupiter, saturn, uranus, neptune
    }

    //每个枚举定义了一个全新的类型
        var directionToHead = CompassPoint.west
    //因为 directionToHead被初始化为  **CompassPoint** 类型，所以之后就可以使用简洁写法给 **directionToHead** 赋值
        directionToHead = .east

//2、使用

    //如何匹配,匹配时必须穷举所有情况，不然会报错，或者使用default
    directionToHead = .south
    switch directionToHead {
        case .north:
            print("lots of plants have a north")
        case .south:
            print("Watch out for penguins")
        case .east:
            print("Where the sun rises")
        case .west:
            print("Where the skies are blue")
    //    default:
    //        print("Not a safe place for humans")
    }


//3、关联值
    enum BarCode{
        case upc(Int,Int ,Int, Int)
        case qrCode(String)
    }

    var productBarcode = BarCode.upc(8, 85909, 51226, 3)
    productBarcode = .qrCode("ABCDEFGHIJKLMNOP")

    switch productBarcode {
        case .upc(let numberSystem, let manufacturer, let product, let check):
            print("UPC: \(numberSystem), \(manufacturer), \(product), \(check).")
        case .qrCode(let productCode):
            print("QR code: \(productCode).")
    }
    //元组中类型相同可以将 let 提取出来
    switch productBarcode {
        case let .upc(numberSystem, manufacturer, product, check):
            print("UPC: \(numberSystem), \(manufacturer), \(product), \(check).")
        case let .qrCode(productCode):
            print("QR code: \(productCode).")
    }


//原始值,就是对关联值进行预初始化,
//与关联值的区别：
    //关联值得成员可以改变，原始值不能改变
    //原始值的所有成员具有相同类型
//原始值的类型必须相同
//原始值可以是字符串，字符，或者任意整型值或浮点型值。每个原始值在枚举声明中必须是唯一的。


    enum ASCIIControlCharacter: Character {
        case tab = "\t"
        case lineFeed = "\n"
        case carriageReturn = "\r"
    }

    //隐式赋值
    //在原始值为整数或者字符串类型的枚举时,不需要显式地为每一个枚举成员设置原始值,隐式赋值的值依次递增1
    //如果第一个枚举成员没有设置原始值，其原始值将为0。

    enum Planet: Int {
        case mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune
    }

    enum CompassPoint1: String {
        case north, south, east, west
    }

    print(CompassPoint1.south)

    //rawValue
    //如果在定义枚举类型的时候使用了原始值，那么将会自动获得一个初始化方法，这个方法接收一个叫做rawValue的参数，参数类型即为原始值类型，返回值则是枚举成员或nil
    //你可以使用这个初始化方法来创建一个新的枚举实例
    let possiblePlanet = Planet(rawValue: 7)//可失败
    let positionToFind = 11
    if let somePlanet = Planet(rawValue: positionToFind) {
        switch somePlanet {
        case .earth:
            print("Mostly harmless")
        default:
            print("Not a safe place for humans")
        }
    } else {
        print("There isn't a planet at position \(positionToFind)")
    }


//递归枚举
//一个或多个枚举成员使用该枚举类型的实例作为关联值
//使用递归枚举时，编译器会插入一个间接层

enum ArithmeticExpression {
    case number(Int)
    indirect case addition(ArithmeticExpression, ArithmeticExpression)
    indirect case multiplication(ArithmeticExpression, ArithmeticExpression)
}
//表示所有成员是可递归的
indirect enum ArithmeticExpression1 {
    case number(Int)
    case addition(ArithmeticExpression1, ArithmeticExpression1)
    case multiplication(ArithmeticExpression1, ArithmeticExpression1)
}

//(4+5)*2
let five = ArithmeticExpression.number(5)
let four = ArithmeticExpression.number(4)
let sum = ArithmeticExpression.addition(five, four)
let product = ArithmeticExpression.multiplication(sum, ArithmeticExpression.number(2))


//要操作具有递归性质的数据结构，使用递归函数是一种直截了当的方式
func evaluate(_ expression: ArithmeticExpression) -> Int {
    switch expression {
        case let .number(value):
            return value
        case let .addition(left, right):
            return evaluate(left) + evaluate(right)
        case let .multiplication(left, right):
            return evaluate(left) * evaluate(right)
    }
}

print(evaluate(product))

